var seckill={
    //封装秒杀相关的URL
    URL:{
        now:function () {
            return '/secKill/time/now';
        },
        exposer:function (goodsId) {
            return '/secKill/'+goodsId+'/exposer';
        },
        execution:function (goodsId,md5,userId) {
            return '/secKill/'+goodsId+'/'+md5+'/'+userId+'/executeMQ';
        },
        login:function () {
            return '/user/login';
        }
    },
    handleSeckillkill:function(goodsId,node,userId){
        node.hide().html('<button class="btn btn-primary btn-lg" id="killBtn">开始秒杀</button>');
        /*建立websocket连接*/
        if(userId!==""){
            var webSocket = new WebSocket("ws://localhost:8080/secKill/"+userId);
            webSocket.onmessage=function(me){
                var dataStr = me.data;
                var result = JSON.parse(dataStr);
                if(result&&result['success']){
                    var killResult=result['data'];
                    var stateInfo=killResult['stateInfo'];
                    node.html('<span class="label label-success">'+stateInfo+'</span>')
                }else{
                    var killResult=result['data'];
                    var stateInfo=killResult['stateInfo'];
                    node.html('<span class="label label-warning">'+stateInfo+'</span>')
                }
            };
        }
        $.post(seckill.URL.exposer(goodsId),{},function (result) {
            if(result&&result['success']){
                var exposer=result['data'];
                if(exposer['exposed']){
                    var md5=exposer['md5'];

                    $('#killBtn').one('click',function () {
                        //1.禁用按钮
                        var killUrl=seckill.URL.execution(goodsId,md5,userId);
                        $(this).addClass('disabled');
                        //2.发送秒杀执行请求
                        $.post(killUrl,{},function (result) {
                            if(result&&result['success']){
                                var killResult=result['data'];
                                var stateInfo=killResult['stateInfo'];
                                node.html('<span class="label label-success">'+stateInfo+'</span>')
                            }else{
                                var killResult=result['data'];
                                var stateInfo=killResult['stateInfo'];
                                node.html('<span class="label label-warning">'+stateInfo+'</span>')
                            }
                        });
                    });

                }else if(exposer['goodsId']==-1){
                    var seckillBox = $('#seckill-box');
                    seckillBox.html('<span class="label label-warning">'+'秒杀已结束!'+'</span>');
                }else{
                    var seckillBox = $('#seckill-box');
                    seckillBox.html('<span class="label label-warning">'+'操作异常'+'</span>');
                }
            }else {
                console.log('result:'+result);
            }
            node.show();
        });
    },
    countdown:function (goodsId,userId,nowTime,startTime,endTime) {
      var seckillBox = $('#seckill-box');
      if(nowTime>endTime){
          seckillBox.html('秒杀已结束!');
      }else if(nowTime<startTime){
          seckillBox.countdown(startTime,function (event) {
              var format=event.strftime('秒杀倒计时：%D天 %H时 %M分 %S秒');
              seckillBox.html(format);
          }).on('finish.countdown',function () {
              seckill.handleSeckillkill(goodsId,seckillBox,userId);
          })
      }else{
          seckill.handleSeckillkill(goodsId,seckillBox,userId);
      }
    },

    //详情页秒杀逻辑
    detail:{
        //详情页初始化
      init:function(params){
          //手机验证和登录，计时交互
          //规划交互流程
          var killPhone=$.cookie("userPhone");
          var userId=params['userId'];
          if(killPhone=="" || userId==""){
              alert("请先登录！");
              $(window).attr("location",seckill.URL.login());
          }
          //已登录
          //进行计时
          var startTime=params['startTime'];
          var endTime=params['endTime'];
          var goodsId=params['goodsId'];
          $.get(seckill.URL.now(),{},function (result) {
                if(result&&result['success']){
                    var nowTime=result['data'];
                    seckill.countdown(goodsId,userId,nowTime,startTime,endTime);
                }else {
                    console.log(result);
                }
          });
      }
    }
};